package com.farm.web.controller;

import com.alibaba.fastjson.JSON;
import com.farm.entity.PageResult;
import com.farm.entity.Result;
import com.farm.entity.StatusCode;
import com.farm.web.pojo.CustUser;
import com.farm.web.service.CustUserService;
import com.farm.web.util.CookieUtil;
import com.farm.web.util.JwtUtil;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/custUser")
public class CustUserController {

    @Autowired
    private CustUserService custUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加用户
     * @param custUser
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody CustUser custUser, @RequestParam(value = "roleIds",required = false) Integer[] roleIds, HttpServletRequest request) throws Exception {
        //获取cookie里的用户
        Map<String, String> map= CookieUtil.readCookie(request, "username");
        String username1 = map.get("username");

        //看redis有没有数据
        if (redisTemplate.opsForValue().get(username1)==null){
            //没有就说明没登录
            throw new RuntimeException("请登录后再操作");
        }
        Object o = redisTemplate.opsForValue().get(username1);
        String s = o.toString();
//        Map<String, Object> map = JwtUtil.parseJWT("haha");
//        Object username1 = map.get("username");
//        String s = username1.toString();
        Map<String, Object> claims = JwtUtil.parseJWT(s);
        Object haha = claims.get("haha");
        if (!"0".equals(haha)){
            return new Result(false,StatusCode.ERROR,"权限不足");
        }

        String username = custUser.getUsername();
        //判断用户名不能为空
        if (StringUtils.isEmpty(username)){
            return new Result(false,StatusCode.ERROR,"用户名不能为空");
        }
        CustUser user = findUser(username);
        //判断用户名不能重复
        if (user!=null){
            return new Result(false,StatusCode.ERROR,"用户名已存在");
        }

        String customer_id = findUser(username1).getCustomer_id();
        custUser.setCustomer_id(customer_id);

        custUserService.addUser(custUser,roleIds);
        try {
            return new Result(true, StatusCode.OK,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加失败");
        }

    }

    //修改密码
    @PutMapping("/updatePassword")
    public Result updatepassword(@RequestBody Map map,HttpServletRequest request) throws Exception {
        Map<String, String> m= CookieUtil.readCookie(request, "username");
        String username = m.get("username");
        Object o = redisTemplate.opsForValue().get(username);
        if (o==null){
            throw new RuntimeException("请登录后再操作");
        }
        CustUser custUser=new CustUser();
        custUser.setUsername(username);
        custUser.setPassword(map.get("oldPassword").toString());
        String newPassword = map.get("newPassword").toString();
        custUserService.updatepassword(custUser,newPassword);
        try {
            return new Result(true, StatusCode.OK,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }

    //删除用户
    @DeleteMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable("id") Integer id,HttpServletRequest request) throws Exception {
        Map<String, String> map= CookieUtil.readCookie(request, "username");
        String username = map.get("username");
        Object o = redisTemplate.opsForValue().get(username);
        if (o==null){
            throw new RuntimeException("请登录后再操作");
        }
        Map<String, Object> claims = JwtUtil.parseJWT(o.toString());
        Object haha = claims.get("haha");
        if (!"0".equals(haha)){
            return new Result(false,StatusCode.ERROR,"权限不足");
        }
        custUserService.deleteUser(id);
        try {
            return new Result(true, StatusCode.OK,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"删除失败");
        }
    }

    //重置密码
    @PutMapping("/resetPassword/{username}")
    public Result resetPassword(@PathVariable("username") String username,HttpServletRequest request) throws Exception {
        Map<String, String> map= CookieUtil.readCookie(request, "username");
        String username1 = map.get("username");
        Object o = redisTemplate.opsForValue().get(username1);
        if (o==null){
            throw new RuntimeException("请登录后再操作");
        }
        Map<String, Object> claims = JwtUtil.parseJWT(o.toString());
        Object haha = claims.get("haha");
        if (!"0".equals(haha)){
            return new Result(false,StatusCode.ERROR,"权限不足");
        }
        custUserService.resetPassword(username);
        try {
            return new Result(true, StatusCode.OK,"重置成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"重置失败");
        }
    }

    //查询该组织的所有用户
    @GetMapping("/findPage")
    public Result findPage(HttpServletRequest request,@RequestParam Map map,@RequestParam(value = "size",defaultValue = "0",required = false) Integer size,@RequestParam(value = "page",defaultValue = "0",required = false) Integer page){
        Map<String, String> map1 = CookieUtil.readCookie(request, "username");
        String username = map1.get("username").toString();
        if (username==null){
            throw new RuntimeException("请登录再操作");
        }
        String type = findUser(username).getType();
        if(!"0".equals(type)){
           throw new RuntimeException("权限不足");
        }
        String customer_id = findUser(username).getCustomer_id();
        PageResult all = custUserService.findAll(customer_id, map, page, size);

        return new Result(true,StatusCode.OK,"查询成功",all);

    }



    //单一查询
    public CustUser findUser(String username){
        CustUser user = custUserService.findUser(username);
        return user;
    }
}
